
function getLocalIPs(callback) {
    const ips = [];
    const RTCPeerConnection = window.RTCPeerConnection ||
        window.mozRTCPeerConnection ||
        window.webkitRTCPeerConnection;

    const pc = new RTCPeerConnection({
        iceServers: []
    });

    pc.createDataChannel("");

    pc.createOffer()
        .then(offer => pc.setLocalDescription(offer))
        .catch(err => console.error(err));

    pc.onicecandidate = (event) => {
        if (!event.candidate) {
            pc.close();
            callback(ips);
            return;
        }
        const ip = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/.exec(event.candidate.candidate)[1];
        if (ips.indexOf(ip)){
            ips.push(ip);
        }
    };
}


async function getPublicIP() {
    const services = [
        'https://ipapi.co/json',
        'https://ipinfo.io/json'
    ];
    console.log(services)
    if (sessionStorage.getItem('publicIP')) {
        return sessionStorage.getItem('publicIP');
    } else {
        for (const url of services) {
            try {
                const response = await axios.get(url);
                const data = response.data;
                let ip = data.ip || data.ip_address;
                sessionStorage.setItem('publicIP', ip);
                return ip;
            } catch (error) {
                console.log(`尝试 ${url} 失败`);
            }
        }
    }
    return null;
}
// // 使用示例
// getLocalIPs(function(ips) {
//     console.log("本地IP地址:", ips);
// });
// getPublicIP().then(publicIP => {
//     console.log("公网IP:", publicIP);
//     return  publicIP;
// });

